// package/pages/areaCode/setAreaCode/setAreaCode.js
var app = getApp();
const cancelText = "取消",
  deleteText = "删除",
  confirmText = "确定";
var is_commit = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    equipment_id: '', //设备id
    name: "", //名称
    area_code: '',
    arrlistA: [],
    index_keyA: 0,
    arrlistB: [],
    index_keyB: 0,
    show: false,
    itemList: [
      "A", "B", "C", "D", "E", "F", 1, 2, 3, 4, 5, 6, 7, 8, 9, cancelText, 0, deleteText
    ],
    numbers: [{
      value: 0,
      checked: false,
    }, {
      value: 0,
      checked: false,
    }, {
      value: "",
      checked: true,
    }, {
      value: "",
      checked: false,
    }, {
      value: "",
      checked: false,
    }, {
      value: "",
      checked: false,
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      equipment_id: options.equipment_id,
    })
    that.getCardMotherKeyConfig(); //获取秘钥信息
  },



  //获取A秘钥和B秘钥
  getCardMotherKeyConfig() {
    var that = this;
    // Ajax请求
    app.ajax({
      url: "Common/Common/getCardMotherKeyConfig",
      data: {
        type: 2
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            arrlistA: res.data.data.key,
            arrlistB: res.data.data.key_b
          });
        } else {
          app.showToast(res.data.message);
        }
      }
    });
  },
  // A密钥
  keyAChange(e) {
    this.setData({
      index_keyA: e.detail.value,
    })
  },
  // B密钥
  keyBChange(e) {
    this.setData({
      index_keyB: e.detail.value,
    })
  },
  //兼容下发区域码
  setAreaCode(e) {
    var that = this;
    var name = e.detail.value.name;
    var area_code = e.detail.value.area_code;
    var index_keyA = that.data.index_keyA;
    var index_keyB = that.data.index_keyB;
    if (name == '') {
      app.showToast('区域码名称不能为空');
      return false;
    }
    if (index_keyA === '') {
      app.showToast('A秘钥不能为空');
      return false;
    }
    if (!is_commit) {
      return false;
    }
    let key_b;
    if (index_keyB !== '') {
      key_b = that.data.arrlistB[index_keyB].value
    }
    is_commit = false;
    // Ajax请求
    app.ajax({
      url: "Common/Equipment/setAreaCode",
      data: {
        equipment_id: this.data.equipment_id,
        type: 2,
        name: name,
        area_code: area_code,
        key: that.data.arrlistA[index_keyA].value,
        key_b
      },
      success: function (res) {
        if (res.data.code == 1000) {
          app.showToast('下发成功');
          setTimeout(() => {
            let pages = getCurrentPages(); // 获取当前页面栈
            let prevPage = pages[pages.length - 2]; // -2 就是你上一页的数据 你上上页的数据就是-3 了以此类推！
            // 直接操作上一个页面  之后返回 
            prevPage.reloadData();
            wx.navigateBack();
          }, 1500)
        } else {
          app.showToast(res.data.message);
        }
        is_commit = true;
      }
    });
  },





  handleClose() {
    this.setData({
      show: false
    })
  },

  showNumberInput(e) {
    wx.hideKeyboard();
    this.setData({
      show: true
    })
  },

  chooseNumber(e) {
    const value = e.currentTarget.dataset.value;
    if (value == cancelText) { //取消
      this.setData({
        show: false
      })
    } else if (value == deleteText) { //删除
      this.deleteNumber();
    } else if (value == confirmText) {
      this.confirmNumber();
    } else {
      this.numberInput(value);
    }
  },


  confirmNumber() {
    let regionCode = [];
    this.data.numbers.forEach(current => regionCode.push(current.value));
    this.setData({
      area_code: regionCode.join(""),
      show: false
    })
  },

  deleteNumber() {
    const numbers = this.data.numbers;
    let index = numbers.findIndex(current => current.checked == true);
    numbers[index].value = "";
    if (index == 0) {
      numbers[index].checked = true;
    } else {
      numbers[index].checked = false;
      numbers[index - 1].checked = true;
    }
    this.setData({
      numbers
    })
    if (index == numbers.length - 1 || index == 0) {
      let idx = this.data.itemList.findIndex(current => current == (index == 0 ? cancelText : confirmText));
      let txt = index == 0 ? confirmText : cancelText;
      if (~idx) {
        this.setData({
          [`itemList[${idx}]`]: txt
        })
      }
    }
  },

  numberInput(value) {
    const numbers = this.data.numbers;
    let index = numbers.findIndex(current => current.checked == true);
    numbers[index].value = value;
    if (index + 1 == numbers.length) {
      numbers[index].checked = true;
    } else {
      numbers[index].checked = false;
      numbers[index + 1].checked = true;
    }
    this.setData({
      numbers
    })
    if (index + 1 == numbers.length || index == 0) {
      let idx = this.data.itemList.findIndex(current => current == (index == 0 ? confirmText : cancelText));
      let txt = index == 0 ? cancelText : confirmText;
      if (~idx) {
        this.setData({
          [`itemList[${idx}]`]: txt
        })
      }
    }
  },
})